package com.ziyun.erp.web.modules.purchase.controller;

import com.ziyun.erp.common.e.purchase.MaterialSupplierStatusEnum;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.purchase.entity.StoreMaterialSupplierEntity;
import com.ziyun.erp.modules.purchase.service.StoreMaterialSupplierService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.UserUtil;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 采购供应商表
 *
 * @author guizhao
 * @email 296588519@qq.com
 * @date 2018-09-21 15:36:18
 */
@RestController
@RequestMapping("purchase/storematerialsupplier")
public class StoreMaterialSupplierController {
    @Autowired
    private StoreMaterialSupplierService storeMaterialSupplierService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("purchase:storematerialsupplier:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = storeMaterialSupplierService.queryPage(params);

        //存放枚举类到userdata
        Map<String, List> mapEnum = new HashMap<>();
        List<Map<String, Object>> materialSupplierStatusEnum = JsonUtils.enumToList(MaterialSupplierStatusEnum.class);
        mapEnum.put("materialSupplierStatusEnum",materialSupplierStatusEnum);

        return R.ok().put("page", page).put("userData", mapEnum);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{msId}")
    @RequiresPermissions("purchase:storematerialsupplier:info")
    public R info(@PathVariable("msId") Integer msId){
        StoreMaterialSupplierEntity storeMaterialSupplier = storeMaterialSupplierService.selectById(msId);

        return R.ok().put("storeMaterialSupplier", storeMaterialSupplier);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("purchase:storematerialsupplier:save")
    public R save(@RequestBody StoreMaterialSupplierEntity storeMaterialSupplier){
        storeMaterialSupplier.setCreateName(""+UserUtils.getCurentUser().getUsername());
        storeMaterialSupplier.setMsName(storeMaterialSupplier.getMsName().trim());
        //采购供应商的审核改成自动审核 审核状态:0.待审核,1.已通过,2.驳回
        storeMaterialSupplier.setStatus(1);
        storeMaterialSupplier.setAuditTime(new Date()); //审核时间
        storeMaterialSupplier.setAuditName(UserUtils.getCurentUser().getUsername());  //审核人

        storeMaterialSupplierService.insert(storeMaterialSupplier);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("purchase:storematerialsupplier:update")
    public R update(@RequestBody StoreMaterialSupplierEntity storeMaterialSupplier){
        ValidatorUtils.validateEntity(storeMaterialSupplier);
        storeMaterialSupplierService.updateAllColumnById(storeMaterialSupplier);//全部更新

        return R.ok();
    }

    /**
     * 审核通过
     */
    @RequestMapping("/auditConfrim")
    public R auditConfrim(@RequestParam("msId") Integer msId){
        StoreMaterialSupplierEntity storeMaterialSupplierEntity = storeMaterialSupplierService.selectById(msId);
        if(storeMaterialSupplierEntity == null || storeMaterialSupplierEntity.getStatus() != MaterialSupplierStatusEnum.WAIT_AUDIT.toInt()){
            return R.error("当前单据状态不允许审核通过!");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("status",MaterialSupplierStatusEnum.ALREADY_AUDIT.toInt());
        map.put("auditName",UserUtil.getCurrentUser().getUsername());
        map.put("auditTime",new Date());
        map.put("updateName",UserUtil.getCurrentUser().getUsername());
//        if(remark == null || remark.length()<=0 || remark.equals("")){
//            map.put("remark",null);
//        }else{
//            map.put("remark",remark.trim());
//        }
        storeMaterialSupplierService.updateByMap(map,msId);
        return R.ok();
    }

    /**
     * 驳回
     */
    @RequestMapping("/reject")
    public R reject(@RequestParam("msId") Integer msId){
        StoreMaterialSupplierEntity storeMaterialSupplierEntity = storeMaterialSupplierService.selectById(msId);
        if(storeMaterialSupplierEntity == null || storeMaterialSupplierEntity.getStatus() != MaterialSupplierStatusEnum.WAIT_AUDIT.toInt()){
            return R.error("当前单据状态不允许驳回!");
        }/*else if(StringUtil.isEmpty(remark)){
            return R.error("驳回备注不允许为空!");
        }*/

        Map<String,Object> map = new HashMap<>();
        map.put("status",MaterialSupplierStatusEnum.REJECT.toInt());
//        map.put("remark",remark.trim());
        map.put("updateName",UserUtil.getCurrentUser().getUsername());

        storeMaterialSupplierService.updateByMap(map,msId);
        return R.ok();
    }

}
